/*
 * @(#) UDAUtility.java 2013-5-28 下午6:02:49
 *
 * Copyright 2013 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.osi.mes.base.util;

 /**
  * 数据转换工具类
  * @author Nemo, 2013-5-28 下午6:02:51
  */
public class DataUtility
{
	private static final Long BOOLEAN_TRUE = (long) 1;
	
	 /**
     * 将UDA字段转为Int型
     */
	public static Integer toInteger(Object data) throws Exception{
    	if(data == null){
    		return 0;
    	}
    	if(data instanceof Long){
    		return ((Long) data).intValue();
    	}
    	else{
    		return Integer.parseInt(data.toString());
    	}
	}
	
	/**
     * 将UDA转换为布尔值
     */
    public static Boolean toBoolean(Object data) throws Exception{
    	if(data == null){
    		return false;
    	}
    	
    	if(data instanceof Long){
    		return ((Long)data).equals(BOOLEAN_TRUE);
    	}
    	else{
    		throw new Exception("该字段不允许转为布尔值");
    	}
    }
    
	 /**
     * 将UDA字段转为Float型
     */
	public static Float toFloat(Object data) throws Exception{
    	if(data == null){
    		return 0.0f;
    	}
    	if(data instanceof Long){
    		return ((Float) data).floatValue();
    	}
    	else{
    		return Float.parseFloat(data.toString());
    	}
	}
}
